<style>
    .dn{
        display: none;
    }
    .attachments{
        display: block;
        float: left;
        overflow:hidden;
        text-align: center;
    }
    .option-item{
        overflow: hidden;
        margin-top: 5px;
        color: #1E9FFF;
        text-align: center;
    }
    .preview{
        cursor: pointer;
    }
    .file-a{
        font-size: 12px;
        word-break: break-all;
        text-overflow: ellipsis;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        margin-top: 5px;
    }
    .cardU {
        background-image: url(../../assets/images/card_u.png);
        background-repeat: no-repeat;
        background-position: center;
        width: calc(100% - 152px);
        height: 150px;
        padding: 0;
    }

    .cardU img {
        width: 100%;
        height: 100%;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">字典类型：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="dictType" name="dictType" lay-filter="dictType">
                                            <option value="">请选择字典类型</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">字典标签：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="labelName" class="layui-input" type="text" placeholder="请输入字典标签"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="dictLabelSearch" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="dictLabelBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="dictLabelTable" lay-filter="dictLabelTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="dictLabelTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="dictLabelForm">
    <form lay-filter="dictLabelForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">标签名称</label>
            <div class="layui-input-block">
                <input name="labelName" placeholder="请输入标签名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标签值</label>
            <div class="layui-input-block">
                <input name="labelValue" placeholder="请输入标签值" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字典类型</label>
            <div class="layui-input-block">
                <input name="dictType" placeholder="请输入字典类型" type="text" class="layui-input" disabled="disabled"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入排序" type="text" class="layui-input" lay-verify="number"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select id="status" name="status"></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入备注" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="dictLabelFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    var $,config;
    layui.use(['layer', 'form', 'table', 'admin','laytpl','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laytpl=layui.laytpl;

        //字典类型
        var dictType = admin.getTempData("dictType");

        //初始化加载字典类型
        admin.req('enterprise/dict/admin/dict/select', {}, function (res) {
            $.each(res.data, function (index, value) {
                $('#dictType').append(new Option(value.name, value.type));// 下拉菜单里添加元素
            });
            $("#dictType").val(dictType);//初始化字典类型默认值
            layui.form.render("select");//重新渲染 固定写法
        }, 'GET');

        // 渲染表格
        var insTb = table.render({
            elem: '#dictLabelTable',
            url: config.base +'enterprise/dict/admin/dictLabel/list',
            where: {dictType:dictType},
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', align: 'center'}
                ,{field: 'labelName', title: '字典标签', align: "center"}
                ,{field: 'labelValue', title: '字典值', align: 'center' }
                ,{field: 'sort', title: '排序', align: 'center' }
                ,{title: '状态', align: 'center',
                    templet: function (d){
                        if(d.status == "0"){
                            return '<span style="color: #00A65A">正常</span>';
                        }else{
                            return '<span style="color: red">停用</span>';
                        }
                    }
                }
                ,{field: 'remark', title: '备注', align: 'center'}
                ,{field: 'createTime', title: '创建时间', align: 'center'}
                ,{align: 'center', toolbar: '#dictLabelTableBar', title: '操作', width: 200, fixed: 'right'}
            ]]
        });

        // 工具条点击事件
        table.on('tool(dictLabelTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(dictLabelTable)', function(obj){
            table.reload('dictLabelTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(dictLabelSearch)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#dictLabelBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '560px',
                offset: '265px',
                title: data ? '修改字典数据' : '添加字典数据',
                content: $('#dictLabelForm').html(),
                success: function () {
                    //加载状态下拉框
                    admin.getDictList("status", "sys_status",data ? data.status : null, "状态");

                    $("input[name='dictType']").val(dictType);

                    if (data){
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function(index, item){
                            if(data.status  == item.text){
                                data.status = item.value;
                            }
                        });
                    }

                    form.val('dictLabelForm', data);
                    // 表单提交事件
                    form.on('submit(dictLabelFormSubmit)', function (d) {
                        layer.load(2);
                        var url='enterprise/dict/admin/dictLabel/add';
                        if(data){
                            url='enterprise/dict/admin/dictLabel/update';
                        }
                        admin.req(url, d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('dictLabelTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '265px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/dict/admin/dictLabel/del/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

    });
</script>